#ifndef __MC31xx_HAL_SPI_H
#define __MC31xx_HAL_SPI_H

#include "mc31xx_hal_def.h"

//SPI初始化结构体
typedef struct
{
    uint32_t Mode;                            //SPI主从选择       
    uint32_t DataSize;                        //数据长度 固定8 不可配置
    uint32_t CLKPolarity;                     //CPOL 时钟极性 串行同步时钟的空闲状态
    uint32_t CLKPhase;                        //CPHA 时钟相位 在第几个跳变沿采样
    uint32_t TxFirstBit;                      //数据从MSB/LSB开始
    uint32_t RxFirstBit;                      //数据从MSB/LSB开始
    uint32_t BaudRate;                        //波特率
    uint32_t PinNSS;                          //CS脚
    uint32_t PinCLK;                          //时钟脚
    uint32_t PinMOSI;                         //主出脚
    uint32_t PinMISO;                         //主入脚
} SPI_InitTypeDef;

//SPI句柄
typedef struct __SPI_HandleTypeDef
{
  GPCOM_TypeDef             *Instance;        //寄存器基址
  SPI_InitTypeDef           Init;             //初始化结构体
  uint8_t                   RxFifoPoint;      //读缓冲写指针

} SPI_HandleTypeDef;


#define  SPI_MODE_MASTER                (0x00000000U)
#define  SPI_MODE_SLAVE                 (0x00000100U)


#define SPI_POLARITY_LOW                (0x00000000U)
#define SPI_POLARITY_HIGH               (0x00000010U)


#define SPI_PHASE_1EDGE                 (0x00000000U)
#define SPI_PHASE_2EDGE                 (0x00000020U)

// #define SPI_NSS_SOFT                    SPI_CR1_SSM
// #define SPI_NSS_HARD_INPUT              (0x00000000U)
// #define SPI_NSS_HARD_OUTPUT             (SPI_CR2_SSOE << 16U)

#define SPI_TXFIRSTBIT_LSB                (0x00000000U)
#define SPI_TXFIRSTBIT_MSB                (0x00000080U)

#define SPI_RXFIRSTBIT_LSB                (0x00000000U)
#define SPI_RXFIRSTBIT_MSB                (0x00000040U)


#define IS_SPI_INSTANCE(INSTANCE) (((INSTANCE) == GPCOM0) || \
                                    ((INSTANCE) == GPCOM1) || \
                                    ((INSTANCE) == GPCOM2) || \
                                    ((INSTANCE) == GPCOM3) || \
                                    ((INSTANCE) == GPCOM4) || \
                                    ((INSTANCE) == GPCOM5) || \
                                    ((INSTANCE) == GPCOM6) || \
                                    ((INSTANCE) == GPCOM7) || \
                                    ((INSTANCE) == GPCOM8) || \
                                    ((INSTANCE) == GPCOM9) || \
                                    ((INSTANCE) == GPCOM10) || \
                                    ((INSTANCE) == GPCOM11))




HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi);
HAL_StatusTypeDef HAL_SPI_SetSpeed(SPI_HandleTypeDef *hspi,uint32_t BaudRate);
HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi);
HAL_StatusTypeDef HAL_SPI_RxFIFO_Clear(SPI_HandleTypeDef *hspi);
HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout);
HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
uint8_t HAL_SPI_TransmitReceiveByte(SPI_HandleTypeDef *hspi, uint8_t TxData, uint32_t Timeout);

#endif
